//
//  GetMoneyController.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/4.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "GetMoneyController.h"
#import "KeepCallBottomView.h"
#import "GetMoneySelectCell.h"
#import "GetMoneyTextFieldCell.h"
#import "GetMoneyInfoModel.h"
@interface GetMoneyController ()
/** <#note#> */
@property (nonatomic, strong) KeepCallBottomView *footerView;


/** <#note#> */
@property (nonatomic, strong) GetMoneyInfoModel *infoModel;

/** <#note#> */

@end

@implementation GetMoneyController

- (GetMoneyInfoModel *)infoModel{
    
    if (!_infoModel) {
        
        _infoModel = [[GetMoneyInfoModel alloc]init];
        
    }
    
    
    
    return _infoModel;
}




- (KeepCallBottomView *)footerView{
    
    if (!_footerView) {
        
        KeepCallBottomView *temp = (KeepCallBottomView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([KeepCallBottomView class]) owner:nil options:nil].firstObject;
        
        [temp.topBtn setTitle:@"确认提现" forState:UIControlStateNormal];
        [temp.topBtn setTitle:@"确认提现" forState:UIControlStateHighlighted];
        
        [temp.topBtn addTarget:self action:@selector(getMoney) forControlEvents:UIControlEventTouchUpInside];
        
        _footerView = temp;
        
        [self.view addSubview:temp];
        
    }
    
    return _footerView;
}


- (void)getMoney{
    [self.view endEditing:YES];
    
    if (self.infoModel.bank_name == nil) {
        
        [self showInforWithStr:@"请填写支行名称"];
        
        
        return;
    }
    
    
    if (self.infoModel.realname == nil) {
        
        [self showInforWithStr:@"请填写持卡人姓名"];
      
        
        return;
    }
    
    
    if (self.infoModel.account_number == nil) {
        
        [self showInforWithStr:@"请填写银行卡号"];
        
        
        return;
    }
    
    
    
    BOOL istelephoneNumber1 = [[NetworkTools shareNetworkTools] deptNumInputShouldNumber:self.infoModel.account_number];
    
    if (!istelephoneNumber1) {
        
        [self showInforWithStr:@"银行卡号格式不正确!"];
        
        return;
        
    }
    
    
    
   
    
    if (self.infoModel.mobile == nil) {
        
        [self showInforWithStr:@"请填写手机号"];

        
        return;
    }
    
    
    BOOL istelephoneNumber = [[NetworkTools shareNetworkTools] isPhoneNumber:self.infoModel.mobile];
    
    if (!istelephoneNumber) {
        
        [self showInforWithStr:@"手机号格式不正确!"];

        return;
        
    }
    


    

    
    
    if (self.infoModel.cash == nil) {
        
        [self showInforWithStr:@"请输入提现金额"];
        
        return;
    }
    
    
    //判断是否是纯数字
    
    if( [self isPureInt:self.infoModel.cash] || [self isPureFloat:self.infoModel.cash])
        
    {
        
        
        if (!(self.infoModel.cash.floatValue > 0)) {
            
            [self showInforWithStr:@"输入金额非法"];

            return;
        }
        
        
        
        
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"uid"] = myViewModel.user.uid;
        parameters[@"cash"] = self.infoModel.cash;
        parameters[@"bank_name"] = self.infoModel.bank_name;
        parameters[@"realname"] = self.infoModel.realname;
        parameters[@"account_number"] = self.infoModel.account_number;
        parameters[@"mobile"] = self.infoModel.mobile;

        [SVProgressHUD showWithStatus:@"正在申请..."];
        
        MJWeakSelf
        [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Member/addMemberBalanceWithdraw" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
            
            if (!error) {
                
                [weakSelf.view endEditing:YES];
                
//                [SVProgressHUD showSuccessWithStatus:@"申请成功!等待处理..."];
                
                [SVProgressHUD dismiss];
                
                [weakSelf showSuccess];
                
//                [weakSelf.navigationController performSelector:@selector(popViewControllerAnimated:) withObject:@1 afterDelay:2.0];
                
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:error.domain];
                
            }
        }];
        
        
        
        
        
        
        
    }else{
        
        
        [self showInforWithStr:@"输入金额非法"];

        
        
        
        
    }
    
  
}






//判断是否为整形：

- (BOOL)isPureInt:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return [scan scanInt:&val] && [scan isAtEnd];
    
}


//判断是否为浮点形：

- (BOOL)isPureFloat:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    float val;
    
    return [scan scanFloat:&val] && [scan isAtEnd];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    
    
}


- (void)showSuccess{
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:@"申请已提交,等待客服处理..." preferredStyle:UIAlertControllerStyleAlert];
    
    MJWeakSelf;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf.navigationController popViewControllerAnimated:YES];

//        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf.navigationController popViewControllerAnimated:YES];

        
//        [weakSelf dismissViewControllerAnimated:YES completion:nil];

        
    }];
    
    
    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
    
}



- (void)showInforWithStr:(NSString *)str{
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
      
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"提现";
    
    [self setNav];
    
    
//    self.tableView.rowHeight = 52;
    
    [self.tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GetMoneySelectCell class]) bundle:nil] forCellReuseIdentifier:[GetMoneySelectCell identifierStr]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GetMoneyTextFieldCell class]) bundle:nil] forCellReuseIdentifier:[GetMoneyTextFieldCell identifierStr]];

    self.tableView.tableFooterView = self.footerView;

}


- (void)setNav{
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    
    [but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    but.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //        but.backgroundColor = [UIColor redColor];
    
    but.frame = CGRectMake(0, 0, 44, 44);
    but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
    
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.leftBarButtonItem = returnButtonItem;
    
    
  
    
    
}


- (void)back{
    
    [self.view endEditing:YES];
    
    [self.navigationController popViewControllerAnimated:YES];

    
//    [self dismissViewControllerAnimated:YES completion:nil];
  
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.row == 5) {
        
        BaseTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return tableCell;
        
    } else {
        
        GetMoneyTextFieldCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[GetMoneyTextFieldCell identifierStr]];
        
        
        tableCell.index = indexPath;
        
        tableCell.model = self.infoModel;
        
        
        return tableCell;
    }
  
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 5) {
        
        return 0;
        
    } else {
        
        return 52;
    }
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self.view endEditing:YES];
}


@end
